{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# analysis\n",
    "\n",
    "## 介绍\n",
    "单因子多维度分析.从因子ic,因子收益,选股潜在收益空间三个维度给出因子评价.新增模块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ic_stats\n",
    "- ` jaqs_fxdayu.research.signaldigger.analysis.ic_stats(signal_data) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 因子ic分析表\n",
    "- 对事件因子(数值为0/1/-1的因子)无法使用该方法\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|signal_data |是|pandas.DataFrame |trade_date+symbol为MultiIndex,columns为signal(因子)、return(持有期相对/绝对收益,必须)、upside_ret(持有期潜在最大上涨收益,非必须)、downside_ret(持有期潜在最大下跌收益,非必须)、group(分组/行业分类,非必须)、quantile(按因子值分组,非必须)|\n",
    "\n",
    "**返回:**\n",
    "因子ic分析表\n",
    "* 列:\n",
    "  * return_ic/upside_ret_ic/downside_ret_ic\n",
    "  * 持有期收益的ic/持有期最大向上空间的ic/持有期最大向下空间的ic\n",
    "  \n",
    "* 行:\n",
    "  *  \"IC Mean\", \"IC Std.\", \"t-stat(IC)\", \"p-value(IC)\", \"IC Skew\", \"IC Kurtosis\", \"Ann. IR\"\n",
    "  * IC均值，IC标准差，IC的t统计量，对IC做0均值假设检验的p-value，IC偏度，IC峰度，iC的年化信息比率-mean/std\n",
    "\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataview loaded successfully.\n",
      "Nan Data Count (should be zero) : 0;  Percentage of effective data: 99%\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>signal</th>\n",
       "      <th>return</th>\n",
       "      <th>upside_ret</th>\n",
       "      <th>downside_ret</th>\n",
       "      <th>group</th>\n",
       "      <th>quantile</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th>symbol</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">20170503</th>\n",
       "      <th>000001.SZ</th>\n",
       "      <td>6.7925</td>\n",
       "      <td>-0.005637</td>\n",
       "      <td>-0.003045</td>\n",
       "      <td>-0.042326</td>\n",
       "      <td>480000</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000002.SZ</th>\n",
       "      <td>10.0821</td>\n",
       "      <td>0.011225</td>\n",
       "      <td>0.016697</td>\n",
       "      <td>-0.029432</td>\n",
       "      <td>430000</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000008.SZ</th>\n",
       "      <td>42.9544</td>\n",
       "      <td>-0.049408</td>\n",
       "      <td>0.000463</td>\n",
       "      <td>-0.092972</td>\n",
       "      <td>640000</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000009.SZ</th>\n",
       "      <td>79.4778</td>\n",
       "      <td>-0.069822</td>\n",
       "      <td>0.009714</td>\n",
       "      <td>-0.095426</td>\n",
       "      <td>510000</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000027.SZ</th>\n",
       "      <td>20.4542</td>\n",
       "      <td>-0.019517</td>\n",
       "      <td>0.009404</td>\n",
       "      <td>-0.041616</td>\n",
       "      <td>410000</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       signal    return  upside_ret  downside_ret   group  \\\n",
       "trade_date symbol                                                           \n",
       "20170503   000001.SZ   6.7925 -0.005637   -0.003045     -0.042326  480000   \n",
       "           000002.SZ  10.0821  0.011225    0.016697     -0.029432  430000   \n",
       "           000008.SZ  42.9544 -0.049408    0.000463     -0.092972  640000   \n",
       "           000009.SZ  79.4778 -0.069822    0.009714     -0.095426  510000   \n",
       "           000027.SZ  20.4542 -0.019517    0.009404     -0.041616  410000   \n",
       "\n",
       "                      quantile  \n",
       "trade_date symbol               \n",
       "20170503   000001.SZ         1  \n",
       "           000002.SZ         1  \n",
       "           000008.SZ         4  \n",
       "           000009.SZ         5  \n",
       "           000027.SZ         2  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.data import DataView\n",
    "from jaqs_fxdayu.research import SignalDigger\n",
    "\n",
    "# 加载dataview数据集\n",
    "dv = DataView()\n",
    "dataview_folder = './data'\n",
    "dv.load_dataview(dataview_folder)\n",
    "\n",
    "# 计算signal_data(通过jaqs.research.signaldigger.digger.SignalDigger.process_signal_before_analysis(*args, **kwargs))\n",
    "sd = SignalDigger()\n",
    "sd.process_signal_before_analysis(signal=dv.get_ts(\"pe\"),\n",
    "                                  price=dv.get_ts(\"close_adj\"),\n",
    "                                  high=dv.get_ts(\"high_adj\"),\n",
    "                                  low=dv.get_ts(\"low_adj\"),\n",
    "                                  group=dv.get_ts(\"sw1\"),\n",
    "                                  n_quantiles=5,\n",
    "                                  period=5,\n",
    "                                  benchmark_price=dv.data_benchmark,\n",
    "                                  )\n",
    "signal_data = sd.signal_data\n",
    "signal_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>return_ic</th>\n",
       "      <th>upside_ret_ic</th>\n",
       "      <th>downside_ret_ic</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>IC Mean</th>\n",
       "      <td>-0.022805</td>\n",
       "      <td>0.031198</td>\n",
       "      <td>-2.035376e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Std.</th>\n",
       "      <td>0.207325</td>\n",
       "      <td>0.159313</td>\n",
       "      <td>1.692702e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t-stat(IC)</th>\n",
       "      <td>-1.105467</td>\n",
       "      <td>1.968055</td>\n",
       "      <td>-1.208439e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>p-value(IC)</th>\n",
       "      <td>0.271610</td>\n",
       "      <td>0.051831</td>\n",
       "      <td>2.894849e-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Skew</th>\n",
       "      <td>0.009493</td>\n",
       "      <td>-0.065715</td>\n",
       "      <td>4.407910e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Kurtosis</th>\n",
       "      <td>-0.978744</td>\n",
       "      <td>-0.639758</td>\n",
       "      <td>-5.878823e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ann. IR</th>\n",
       "      <td>-0.109998</td>\n",
       "      <td>0.195829</td>\n",
       "      <td>-1.202442e+00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             return_ic  upside_ret_ic  downside_ret_ic\n",
       "IC Mean      -0.022805       0.031198    -2.035376e-01\n",
       "IC Std.       0.207325       0.159313     1.692702e-01\n",
       "t-stat(IC)   -1.105467       1.968055    -1.208439e+01\n",
       "p-value(IC)   0.271610       0.051831     2.894849e-21\n",
       "IC Skew       0.009493      -0.065715     4.407910e-01\n",
       "IC Kurtosis  -0.978744      -0.639758    -5.878823e-01\n",
       "Ann. IR      -0.109998       0.195829    -1.202442e+00"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.analysis import ic_stats\n",
    "\n",
    "ic_stats(signal_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### return_stats\n",
    "- ` jaqs_fxdayu.research.signaldigger.analysis.return_stats(signal_data,is_event,period) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 因子收益分析表--根据因子构建几种投资组合，通过组合表现分析因子的收益能力\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|signal_data |是|pandas.DataFrame |trade_date+symbol为MultiIndex,columns为signal(因子)、return(持有期相对/绝对收益,必须)、upside_ret(持有期潜在最大上涨收益,非必须)、downside_ret(持有期潜在最大下跌收益,非必须)、group(分组/行业分类,非必须)、quantile(按因子值分组,非必须)|\n",
    "|is_event |是|bool |是否是事件因子(数值为0/1/-1的因子)|\n",
    "|period |是|int |换仓周期(天数),**注意:**必须与signal_data中收益的计算周期一致|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "收益分析表\n",
    "* 列:\n",
    "  * long_ret/short_ret/long_short_ret/top_quantile_ret/bottom_quantile_ret/tmb_ret/all_sample_ret\n",
    "  * 多头组合收益/空头组合收益/多空组合收益/因子值最大组合收益/因子值最小组合收益/因子值最大组（构建多头）+因子值最小组（构建空头）收益/全样本（无论信号大小和方向）-基准组合收益\n",
    "  \n",
    "* 行:\n",
    "  * 't-stat', \"p-value\", \"skewness\", \"kurtosis\", \"Ann. Ret\", \"Ann. Vol\", \"Ann. IR\", \"occurance\"\n",
    "  * 持有期收益的t统计量，对持有期收益做0均值假设检验的p-value，偏度，峰度，持有期收益年化值，年化波动率，年化信息比率-年化收益/年化波动率，样本数量\n",
    "\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>long_ret</th>\n",
       "      <th>long_short_ret</th>\n",
       "      <th>top_quantile_ret</th>\n",
       "      <th>bottom_quantile_ret</th>\n",
       "      <th>tmb_ret</th>\n",
       "      <th>all_sample_ret</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>t-stat</th>\n",
       "      <td>-1.203846</td>\n",
       "      <td>0.411628</td>\n",
       "      <td>-4.728619</td>\n",
       "      <td>-2.714885</td>\n",
       "      <td>-0.755901</td>\n",
       "      <td>-12.043624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>p-value</th>\n",
       "      <td>0.231360</td>\n",
       "      <td>0.681450</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.006650</td>\n",
       "      <td>0.451400</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>skewness</th>\n",
       "      <td>-0.083057</td>\n",
       "      <td>0.373680</td>\n",
       "      <td>0.495042</td>\n",
       "      <td>1.348467</td>\n",
       "      <td>-0.261998</td>\n",
       "      <td>0.546392</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>kurtosis</th>\n",
       "      <td>-0.555038</td>\n",
       "      <td>0.042535</td>\n",
       "      <td>6.187667</td>\n",
       "      <td>9.207208</td>\n",
       "      <td>-0.272022</td>\n",
       "      <td>6.241350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ann. Ret</th>\n",
       "      <td>-0.101735</td>\n",
       "      <td>0.021452</td>\n",
       "      <td>-0.129940</td>\n",
       "      <td>-0.051046</td>\n",
       "      <td>-0.078894</td>\n",
       "      <td>-0.120509</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ann. Vol</th>\n",
       "      <td>0.124471</td>\n",
       "      <td>0.076759</td>\n",
       "      <td>0.330355</td>\n",
       "      <td>0.226040</td>\n",
       "      <td>0.153727</td>\n",
       "      <td>0.268994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ann. IR</th>\n",
       "      <td>-0.817333</td>\n",
       "      <td>0.279469</td>\n",
       "      <td>-0.393336</td>\n",
       "      <td>-0.225829</td>\n",
       "      <td>-0.513207</td>\n",
       "      <td>-0.447998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>occurance</th>\n",
       "      <td>106.000000</td>\n",
       "      <td>106.000000</td>\n",
       "      <td>6996.000000</td>\n",
       "      <td>6996.000000</td>\n",
       "      <td>106.000000</td>\n",
       "      <td>34980.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             long_ret  long_short_ret  top_quantile_ret  bottom_quantile_ret  \\\n",
       "t-stat      -1.203846        0.411628         -4.728619            -2.714885   \n",
       "p-value      0.231360        0.681450          0.000000             0.006650   \n",
       "skewness    -0.083057        0.373680          0.495042             1.348467   \n",
       "kurtosis    -0.555038        0.042535          6.187667             9.207208   \n",
       "Ann. Ret    -0.101735        0.021452         -0.129940            -0.051046   \n",
       "Ann. Vol     0.124471        0.076759          0.330355             0.226040   \n",
       "Ann. IR     -0.817333        0.279469         -0.393336            -0.225829   \n",
       "occurance  106.000000      106.000000       6996.000000          6996.000000   \n",
       "\n",
       "              tmb_ret  all_sample_ret  \n",
       "t-stat      -0.755901      -12.043624  \n",
       "p-value      0.451400        0.000000  \n",
       "skewness    -0.261998        0.546392  \n",
       "kurtosis    -0.272022        6.241350  \n",
       "Ann. Ret    -0.078894       -0.120509  \n",
       "Ann. Vol     0.153727        0.268994  \n",
       "Ann. IR     -0.513207       -0.447998  \n",
       "occurance  106.000000    34980.000000  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.analysis import return_stats\n",
    "\n",
    "return_stats(signal_data,is_event=False,period=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## space_stats\n",
    "- ` jaqs_fxdayu.research.signaldigger.analysis.space_stats(signal_data,is_event) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 因子潜在收益空间分析表--根据因子构建几种投资组合，通过组合在换仓周期内可能达到潜在最大上涨空间、潜在最大下跌空间来分析该因子选股收益的提升潜力，用于进一步辅助设计择时方案\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|signal_data |是|pandas.DataFrame |trade_date+symbol为MultiIndex,columns为signal(因子)、return(持有期相对/绝对收益,必须)、upside_ret(持有期潜在最大上涨收益,非必须)、downside_ret(持有期潜在最大下跌收益,非必须)、group(分组/行业分类,非必须)、quantile(按因子值分组,非必须)|\n",
    "|is_event |是|bool |是否是事件因子(数值为0/1/-1的因子)|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "因子潜在收益空间分析表\n",
    "* 列:\n",
    "  * long_space/short_space/long_short_space/top_quantile_space/bottom_quantile_space/tmb_space/all_sample_space\n",
    "  * 多头组合空间/空头组合空间/多空组合空间/因子值最大组合空间/因子值最小组合空间/因子值最大组（构建多头）+因子值最小组（构建空头）空间/全样本（无论信号大小和方向）-基准组合空间\n",
    "  \n",
    "* 行:\n",
    "  * 'Up_sp Mean','Up_sp Std','Up_sp IR','Up_sp Pct5', 'Up_sp Pct25 ','Up_sp Pct50 ', 'Up_sp Pct75','Up_sp Pct95','Up_sp Occur','Down_sp Mean','Down_sp Std', 'Down_sp IR', 'Down_sp Pct5','Down_sp Pct25 ','Down_sp Pct50 ','Down_sp Pct75', 'Down_sp Pct95','Down_sp Occur'\n",
    "  * 组合持有个股的上行空间均值，上行空间标准差，上行空间信息比率-均值/标准差，上行空间5%分位数,..25%分位数，..中位数，..75%分位数,..95%分位数，上行空间样本数，下行空间...(同上行空间)\n",
    "\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>long_space</th>\n",
       "      <th>top_quantile_space</th>\n",
       "      <th>bottom_quantile_space</th>\n",
       "      <th>tmb_space</th>\n",
       "      <th>all_sample_space</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Up_sp Mean</th>\n",
       "      <td>-0.091582</td>\n",
       "      <td>-0.089756</td>\n",
       "      <td>-0.016239</td>\n",
       "      <td>-0.013714</td>\n",
       "      <td>-0.026786</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Up_sp Std</th>\n",
       "      <td>0.033321</td>\n",
       "      <td>0.343245</td>\n",
       "      <td>0.212997</td>\n",
       "      <td>0.017699</td>\n",
       "      <td>0.240319</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Up_sp IR</th>\n",
       "      <td>-2.748454</td>\n",
       "      <td>-0.261492</td>\n",
       "      <td>-0.076242</td>\n",
       "      <td>-0.774819</td>\n",
       "      <td>-0.111460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Up_sp Pct5</th>\n",
       "      <td>-0.127152</td>\n",
       "      <td>-1.000800</td>\n",
       "      <td>-0.005893</td>\n",
       "      <td>-0.040333</td>\n",
       "      <td>-1.000800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Up_sp Pct25</th>\n",
       "      <td>-0.117286</td>\n",
       "      <td>0.002457</td>\n",
       "      <td>0.004533</td>\n",
       "      <td>-0.028591</td>\n",
       "      <td>0.005062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Up_sp Pct50</th>\n",
       "      <td>-0.101419</td>\n",
       "      <td>0.020756</td>\n",
       "      <td>0.017939</td>\n",
       "      <td>-0.013746</td>\n",
       "      <td>0.019105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Up_sp Pct75</th>\n",
       "      <td>-0.076478</td>\n",
       "      <td>0.047980</td>\n",
       "      <td>0.039831</td>\n",
       "      <td>-0.000051</td>\n",
       "      <td>0.041935</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Up_sp Pct95</th>\n",
       "      <td>-0.031515</td>\n",
       "      <td>0.111557</td>\n",
       "      <td>0.090402</td>\n",
       "      <td>0.013496</td>\n",
       "      <td>0.098799</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Up_sp Occur</th>\n",
       "      <td>106.000000</td>\n",
       "      <td>6996.000000</td>\n",
       "      <td>6996.000000</td>\n",
       "      <td>106.000000</td>\n",
       "      <td>34980.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Down_sp Mean</th>\n",
       "      <td>-0.167327</td>\n",
       "      <td>-0.171114</td>\n",
       "      <td>-0.076042</td>\n",
       "      <td>-0.154875</td>\n",
       "      <td>-0.092512</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Down_sp Std</th>\n",
       "      <td>0.046346</td>\n",
       "      <td>0.340002</td>\n",
       "      <td>0.224699</td>\n",
       "      <td>0.045501</td>\n",
       "      <td>0.245442</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Down_sp IR</th>\n",
       "      <td>-3.610429</td>\n",
       "      <td>-0.503275</td>\n",
       "      <td>-0.338419</td>\n",
       "      <td>-3.403795</td>\n",
       "      <td>-0.376919</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Down_sp Pct5</th>\n",
       "      <td>-0.220840</td>\n",
       "      <td>-1.000800</td>\n",
       "      <td>-1.000800</td>\n",
       "      <td>-0.208216</td>\n",
       "      <td>-1.000800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Down_sp Pct25</th>\n",
       "      <td>-0.190647</td>\n",
       "      <td>-0.067406</td>\n",
       "      <td>-0.034329</td>\n",
       "      <td>-0.183180</td>\n",
       "      <td>-0.042842</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Down_sp Pct50</th>\n",
       "      <td>-0.176590</td>\n",
       "      <td>-0.029282</td>\n",
       "      <td>-0.017467</td>\n",
       "      <td>-0.162556</td>\n",
       "      <td>-0.021792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Down_sp Pct75</th>\n",
       "      <td>-0.152016</td>\n",
       "      <td>-0.012810</td>\n",
       "      <td>-0.007824</td>\n",
       "      <td>-0.139399</td>\n",
       "      <td>-0.009769</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Down_sp Pct95</th>\n",
       "      <td>-0.111972</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.086766</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Down_sp Occur</th>\n",
       "      <td>106.000000</td>\n",
       "      <td>6996.000000</td>\n",
       "      <td>6996.000000</td>\n",
       "      <td>106.000000</td>\n",
       "      <td>34980.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               long_space  top_quantile_space  bottom_quantile_space  \\\n",
       "Up_sp Mean      -0.091582           -0.089756              -0.016239   \n",
       "Up_sp Std        0.033321            0.343245               0.212997   \n",
       "Up_sp IR        -2.748454           -0.261492              -0.076242   \n",
       "Up_sp Pct5      -0.127152           -1.000800              -0.005893   \n",
       "Up_sp Pct25     -0.117286            0.002457               0.004533   \n",
       "Up_sp Pct50     -0.101419            0.020756               0.017939   \n",
       "Up_sp Pct75     -0.076478            0.047980               0.039831   \n",
       "Up_sp Pct95     -0.031515            0.111557               0.090402   \n",
       "Up_sp Occur    106.000000         6996.000000            6996.000000   \n",
       "Down_sp Mean    -0.167327           -0.171114              -0.076042   \n",
       "Down_sp Std      0.046346            0.340002               0.224699   \n",
       "Down_sp IR      -3.610429           -0.503275              -0.338419   \n",
       "Down_sp Pct5    -0.220840           -1.000800              -1.000800   \n",
       "Down_sp Pct25   -0.190647           -0.067406              -0.034329   \n",
       "Down_sp Pct50   -0.176590           -0.029282              -0.017467   \n",
       "Down_sp Pct75   -0.152016           -0.012810              -0.007824   \n",
       "Down_sp Pct95   -0.111972            0.000000               0.000000   \n",
       "Down_sp Occur  106.000000         6996.000000            6996.000000   \n",
       "\n",
       "                tmb_space  all_sample_space  \n",
       "Up_sp Mean      -0.013714         -0.026786  \n",
       "Up_sp Std        0.017699          0.240319  \n",
       "Up_sp IR        -0.774819         -0.111460  \n",
       "Up_sp Pct5      -0.040333         -1.000800  \n",
       "Up_sp Pct25     -0.028591          0.005062  \n",
       "Up_sp Pct50     -0.013746          0.019105  \n",
       "Up_sp Pct75     -0.000051          0.041935  \n",
       "Up_sp Pct95      0.013496          0.098799  \n",
       "Up_sp Occur    106.000000      34980.000000  \n",
       "Down_sp Mean    -0.154875         -0.092512  \n",
       "Down_sp Std      0.045501          0.245442  \n",
       "Down_sp IR      -3.403795         -0.376919  \n",
       "Down_sp Pct5    -0.208216         -1.000800  \n",
       "Down_sp Pct25   -0.183180         -0.042842  \n",
       "Down_sp Pct50   -0.162556         -0.021792  \n",
       "Down_sp Pct75   -0.139399         -0.009769  \n",
       "Down_sp Pct95   -0.086766          0.000000  \n",
       "Down_sp Occur  106.000000      34980.000000  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.analysis import space_stats\n",
    "\n",
    "space_stats(signal_data,is_event=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## analysis\n",
    "- ` jaqs_fxdayu.research.signaldigger.analysis.analysis(signal_data,is_event,period) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 同时获得因子ic分析表、收益分析表、潜在收益空间分析表——单独计算三张表的方法见上述api\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|signal_data |是|pandas.DataFrame |trade_date+symbol为MultiIndex,columns为signal(因子)、return(持有期相对/绝对收益,必须)、upside_ret(持有期潜在最大上涨收益,非必须)、downside_ret(持有期潜在最大下跌收益,非必须)、group(分组/行业分类,非必须)、quantile(按因子值分组,非必须)|\n",
    "|is_event |是|bool |是否是事件因子(数值为0/1/-1的因子)|\n",
    "|period |是|int |换仓周期(天数),**注意:**必须与signal_data中收益的计算周期一致|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "由因子ic分析表、收益分析表、潜在收益空间分析表组成的字典(dict)\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['ic', 'ret', 'space'])\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>return_ic</th>\n",
       "      <th>upside_ret_ic</th>\n",
       "      <th>downside_ret_ic</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>IC Mean</th>\n",
       "      <td>-0.022805</td>\n",
       "      <td>0.031198</td>\n",
       "      <td>-2.035376e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Std.</th>\n",
       "      <td>0.207325</td>\n",
       "      <td>0.159313</td>\n",
       "      <td>1.692702e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t-stat(IC)</th>\n",
       "      <td>-1.105467</td>\n",
       "      <td>1.968055</td>\n",
       "      <td>-1.208439e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>p-value(IC)</th>\n",
       "      <td>0.271610</td>\n",
       "      <td>0.051831</td>\n",
       "      <td>2.894849e-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Skew</th>\n",
       "      <td>0.009493</td>\n",
       "      <td>-0.065715</td>\n",
       "      <td>4.407910e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Kurtosis</th>\n",
       "      <td>-0.978744</td>\n",
       "      <td>-0.639758</td>\n",
       "      <td>-5.878823e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ann. IR</th>\n",
       "      <td>-0.109998</td>\n",
       "      <td>0.195829</td>\n",
       "      <td>-1.202442e+00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             return_ic  upside_ret_ic  downside_ret_ic\n",
       "IC Mean      -0.022805       0.031198    -2.035376e-01\n",
       "IC Std.       0.207325       0.159313     1.692702e-01\n",
       "t-stat(IC)   -1.105467       1.968055    -1.208439e+01\n",
       "p-value(IC)   0.271610       0.051831     2.894849e-21\n",
       "IC Skew       0.009493      -0.065715     4.407910e-01\n",
       "IC Kurtosis  -0.978744      -0.639758    -5.878823e-01\n",
       "Ann. IR      -0.109998       0.195829    -1.202442e+00"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.analysis import analysis\n",
    "\n",
    "result = analysis(signal_data,is_event=False,period=5)\n",
    "print(result.keys())\n",
    "result[\"ic\"]"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:IIA]",
   "language": "python",
   "name": "conda-env-IIA-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
